<%@page import="cn.kgc.news.service.impl.DetailServiceImpl"%>
<%@page import="cn.kgc.news.service.DetailService"%>
<%@page import="java.util.Iterator"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="java.util.List"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@page import="java.io.File"%>
<%@page import="cn.kgc.news.pojo.Detail"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="javax.print.attribute.standard.Severity"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		//接收前端发送过来的值
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("utf-8");
		//是否上传成功
		boolean bUpload = false;
		//上传文件的名字
		String uploadFileName = "";
		//每个节点都有自己的名字
		String fieldName = "";
		//判断请求是否是文件上传的请求
		boolean isMultipar = ServletFileUpload.isMultipartContent(request);
		//创建一个接收数据的对象
		Detail detail = new Detail();
		//文件上传的路径
		String uploadFilePath = request.getSession().getServletContext().getRealPath("upload/");
		System.out.println("==========>文件上传路径:" + uploadFilePath);
		//创建一个文件对象判断文件上传的路径是否存在
		File saveDir = new File(uploadFilePath);
		if (!saveDir.exists()) {
			//上传路径不存在
			saveDir.mkdir();
		}
		if (isMultipar) {
			//当前请求是文件上传请求
			//创建文件上传核心工厂
			FileItemFactory factory = new DiskFileItemFactory();
			ServletFileUpload uplod = new ServletFileUpload(factory);

			List<FileItem> items = uplod.parseRequest(request);
			Iterator<FileItem> iter = items.iterator();
			while (iter.hasNext()) {
				FileItem item = (FileItem) iter.next();
				if (item.isFormField()) {
					//当前节点是普通表单
					//获取每个节点的名字
					fieldName = item.getFieldName();
					if (fieldName.equals("title")) {
						detail.setTitle(item.getString("UTF-8"));
					} else if (fieldName.equals("categoryId")) {
						detail.setCategoryId(Integer.parseInt(item.getString()));
					} else if (fieldName.equals("summary")) {
						detail.setSummary(item.getString("UTF-8"));
					} else if (fieldName.equals("newscontent")) {
						detail.setContent(item.getString("UTF-8"));
					} else if (fieldName.equals("author")) {
						detail.setAuth(item.getString("UTF-8"));
					}
				} else {
					//文件上传在这里
					//获取文件名
					String fileName = item.getName();
					if (fileName != null && !fileName.equals("")) {
						//文件名不为空
						File fullName = new File(fileName);
						File saveFile = new File(uploadFilePath, fullName.getName());
						//写入文件
						item.write(saveFile);
						//获取文件的名字
						uploadFileName = fullName.getName();
						//设置图片路径
						detail.setPicPath(uploadFileName);
						bUpload = true;
					}
				}
			}
		} else {
			//当前请求不是文件上传请求
		}
		DetailService service = new DetailServiceImpl();
		int i = service.addDetail(detail);
		if(i > 0){
			response.sendRedirect(request.getContextPath()+"/admin/newsDetailList.jsp");
		}else{
			response.sendRedirect(request.getContextPath()+"/admin/newsDetailCreateSimple.jsp");
		}
	%>
</body>
</html>